﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using EPiServer.DataAbstraction;
using EPiServer.Personalization.VisitorGroups;
using EPiServer.Web.Mvc.VisitorGroups;

namespace CriteriaPack.SelectedLanguageCriterion
{
    public class SelectedLanguageModel : CriterionModelBase
    {
        [Required, DojoWidget(SelectionFactoryType = typeof(EnumSelectionFactory))]
        public SelectedLanguageCondition Condition { get; set; }
        [Required, DojoWidget(SelectionFactoryType = typeof(EnabledLanguageFactory))]
        public string SelectedLanguage { get; set; }

        public override ICriterionModel Copy()
        {
            return base.ShallowCopy();
        }
    }

    public enum SelectedLanguageCondition
    {
        IsViewingIn,
        IsNotViewingIn
    }

    public class EnabledLanguageFactory : ISelectionFactory
    {
        IEnumerable<System.Web.Mvc.SelectListItem> ISelectionFactory.GetSelectListItems(System.Type property)
        {
            var enabledLangs = from l in LanguageBranch.ListEnabled()
                               select new SelectListItem() { Value = l.LanguageID, Text = l.Name }; 

            return enabledLangs;
        }
    }

}